/*
 * @(#)UniqueIdentifierTransformer.java
 * Date 2013-08-17
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.transformer.model;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import oasis.names.tc.ebxml_regrep.xsd.rim._3.ExternalIdentifierType;

import org.apache.commons.lang3.StringUtils;

import us.hornerscorners.lamppost.exception.ConversionException;

import us.hornerscorners.lamppost.model.common.InstanceIdentifier;

import us.hornerscorners.lamppost.registry.utils.XDSUtils;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class UniqueIdentifierTransformer
        extends AbstractTransformer<ExternalIdentifierType, InstanceIdentifier> {

    /** Field description */
    @Inject
    private LocalizedStringTransformer localizedStringTransformer;

    /**
     * Method description
     *
     *
     * @param eid
     *
     * @return
     *
     * @throws ConversionException
     */
    @Override
    public InstanceIdentifier transform(ExternalIdentifierType eid)
            throws ConversionException {

        InstanceIdentifier result = new InstanceIdentifier();

        result.setRoot(eid.getValue());

        result.setEntryURN(XDSUtils.assignEntryUUID(eid.getId()));

        // versioning
        result.setEntryLogicalURN(StringUtils.defaultIfBlank(eid.getLid(),
                result.getEntryURN()));
        result.setEntryVersion(
            StringUtils.defaultIfBlank(
                XDSUtils.parseVersionName(eid.getVersionInfo()), "1"));

        result.getDisplayNames().addAll(
            this.localizedStringTransformer.transform(eid.getName()));
        result.getDescriptions().addAll(
            this.localizedStringTransformer.transform(eid.getDescription()));

        return result;
    }
}
